from pymysql import *
import time
import hashlib
import random
import sys

def get_keys():
    now_time = str(time.time()*1000).encode("utf8") + bytes([random.randint(1,128) for i in range(10)])
    # print("now_time="+now_time)
    m = hashlib.md5(now_time)
    return m.hexdigest()


def main():
    keys_len = 20
    if sys.argv[1]!=None and sys.argv[1].isdigit():
        keys_len = int(sys.argv[1])
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,database='blackfish',user='root',password='',charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()
    # 执行insert语句，并返回受影响的行数：添加一条数据
    # 增加
    count = 0
    for i in range(keys_len):
        key = get_keys()
        print("key="+key)
        count += cs1.execute(
            'insert into device_keys(initkey) values("{}")'.format(key))
        
    print("成功添加{}条数据".format(count))
    # 提交之前的操作，如果之前已经之执行过多次的execute，那么就都进行提交
    conn.commit()

    # 关闭Cursor对象
    cs1.close()
    # 关闭Connection对象
    conn.close()


if __name__=="__main__":
    main()
